package com.sx.micowell.module.learned

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.SPUtils
import com.gyf.immersionbar.ImmersionBar
import com.example.mvvm.bar.TitleBar
import com.miloyu.modelnet.response.LearnedCourseVO
import com.sx.micowell.R
import com.sx.micowell.databinding.FragLearnedCourseBinding
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.LearnedCourseModel

/**
 * @Author: YY
 * @Time: 2021/8/10
 * @Describe 学过的课
 */
@Route(path = RoutePath.LEARNED_COURSE)
class LearnedCourseFragment : MyFragment<FragLearnedCourseBinding, ContainerActivity>(), TitleBarAction {

    lateinit var viewModel: LearnedCourseModel

    private var learnCourseListAdapter : LearnCourseListAdapter? = null

    override fun initViewModel() {
        viewModel = getFragmentScopeViewModel(LearnedCourseModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_learned_course)
    }

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    override fun initData() {
        viewModel.learnedCourse()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        setLearnCourseList()

    }

    override fun getTitleBar(): TitleBar {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        myActivity?.finish()
    }

    private fun setLearnCourseList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerCourse.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerCourse.layoutManager = LinearLayoutManager(context)

        viewModel.courseListData.observe(this) {
            if (it != null && it.size > 0) {
                binding.layoutEmpty.visibility = View.GONE
                if (learnCourseListAdapter == null) {
                    learnCourseListAdapter = LearnCourseListAdapter()
                    learnCourseListAdapter!!.data.clear()
                    learnCourseListAdapter!!.data = it
                    binding.recyclerCourse.adapter = learnCourseListAdapter

                    learnCourseListAdapter!!.setOnItemClickListener { adapter, view, position ->
                        val helpInfoBean: LearnedCourseVO = adapter.getItem(position) as LearnedCourseVO
                    }
                } else {
                    learnCourseListAdapter!!.data = it
                    binding.recyclerCourse.adapter = learnCourseListAdapter
                }
            } else {
                binding.recyclerCourse.adapter = null
                binding.layoutEmpty.visibility = View.VISIBLE
            }

        }
    }
}